Namespace("org.wattdepot.gdata.makahiki");

// Creates the status summary pane for the Makahiki application. 
org.wattdepot.gdata.makahiki.PowerMonitor = function(configuration) {

  // Parameters for this visualization.
  var width = configuration.powerMonitorWidth + 'px';
  var green = configuration.greenIndicatorColor.replace("#", "");
  var yellow = configuration.yellowIndicatorColor.replace("#", "");
  var red = configuration.redIndicatorColor.replace("#","");
  var configuration = configuration;

  // Top-level interface to this visualization.
  function draw(datatable, baselinetable, source, id) {
    var element = document.getElementById(id);
    addTopLevelStyle(element);
    addTitleDiv(element, id, datatable, source);
    addCurrentPowerDiv(element, id, datatable, baselinetable, source);
    addLastUpdateDiv(element, id, datatable, source);
  }

  // Adds 'global' CSS styling to the top-level div passed into this instance.
  function addTopLevelStyle(element) {
    element.style.width = width;
    element.style.backgroundColor = configuration.widgetBackgroundColor;
    element.style.borderWidth = '2px';
    element.style.borderColor = configuration.widgetHeaderColor;
    element.style.borderStyle = 'solid';
    element.className = 'ui-corner-all';

  }

  // Adds standard div CSS styling.
  function addDivStyle(element) {
    element.style.width = width;
    element.style.textAlign = 'center';
  }

  // Styling for the baseline comment and last update divs.
  function addInfoStyle(element) {
    element.style.fontSize = '1.0em';
    element.style.fontWeight = 'normal';
    element.style.width = width;
    element.style.textAlign = 'center';
  }

  // Create the title div (if not already created).
  function addTitleDiv(element, id, datatable, source) {
    var titleId = id + '__Title';
    var div = getElementByIdOrCreate(titleId, 'div');
    addDivStyle(div);
    // Special CSS just for the title.
    //div.style.backgroundColor = configuration.widgetHeaderColor;
    //div.style.color = configuration.widgetHeaderTextColor;
    //div.style.fontVariant = 'small-caps'; // not always supported on systems.
    div.style.fontFamily = 'Verdana, Geneva, sans-serif';
    div.style.fontWeight = 'normal';
    div.style.fontWeight = 'bold';
    div.style.fontSize = '1.5em';

    var row = configuration.getCurrentPowerDataRow(source);
    var column = configuration.currentPowerDataNameColumn;
    div.innerHTML = datatable.getFormattedValue(row, column);
    element.appendChild(div);
  }

  // Create the current power div (if not already created).
  function addCurrentPowerDiv(element, id, datatable, baselinetable, source) {
    var currPowerId = id + '__CurrPower';
    var div = getElementByIdOrCreate(currPowerId, 'div');
    addDivStyle(div);
    // Get current power
    var row = configuration.getCurrentPowerDataRow(source);
    var column = configuration.currentPowerDataPowerColumn;
    var currentPower = datatable.getFormattedValue(row, column);
    // Get baseline power
    var baselineRow = configuration.getBaselineDataRow(source);
    var baselineColumn = configuration.getBaselineDataColumn();
    var baselinePower = baselinetable.getFormattedValue(baselineRow, baselineColumn);

    // Set the color of the power based upon the baseline vs. power comparaison
    
    var dataColor = configuration.getBaselineDataColor(currentPower, baselinePower);
    div.style.color = dataColor;
    var exclamation = 'Uh oh. ';
    if (dataColor === configuration.redIndicatorColor) {
      exclamation = 'Yikes! ';
    } else if (dataColor === configuration.greenIndicatorColor) {
      exclamation = 'Excellent! ';
    }
    div.style.fontSize = '2.0em';
    div.style.fontFamily = 'Verdana,sansSerif';
    div.style.fontWeight = 'bold';
    div.innerHTML = currentPower + ' kW';
    element.appendChild(div);
    var info = getElementByIdOrCreate(currPowerId + '_Info', 'div');
    addInfoStyle(info);
    info.innerHTML = exclamation + configuration.getBaselineDataComment(currentPower, baselinePower) + '.';
    info.style.color = configuration.getBaselineDataColor(currentPower, baselinePower);
    element.appendChild(info);
  }

  // Create the Last update div (if not already created).
  function addLastUpdateDiv(element, id, datatable, source) {
    var lastUpdateId = id + '__LastUpdate';
    var div = getElementByIdOrCreate(lastUpdateId, 'div');
    addInfoStyle(div);
    var row = configuration.getCurrentPowerDataRow(source);
    var column = configuration.currentPowerDataLastUpdateColumn;
    div.innerHTML = datatable.getFormattedValue(row, column);
    div.style.marginBottom = '10px';
    element.appendChild(div);
  }

  // Returns the pre-existing element with id 'id', or else creates and returns a new element with type elementType and that id.
  function getElementByIdOrCreate(id, elementType) {
    var element = document.getElementById(id);
    if (element) {
      return element;
    }
    else {
      element = document.createElement(elementType);
      element.setAttribute("id", id);
      return element;

    }
  }

  return {
    // Public interface to this function. 
    draw : draw
  };
}



